<?php
/*
 *  Jiba: A small collection of classes to make php easier.
 *  Copyright (C) 2007  Dimitri Michaux <dimitri.michaux@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * dynamictext.class.php, Jiba
 * 
 * Allows you to make dynamic text. It replaces %keys% by a value of your
 * choice.
 *
 * @author Dimitri Michaux
 * @version 0.1
 * @package jiba
 */

class DynamicText {
	private $_text;
	private $_arguments;
	
	public function __construct(){
		$this->_text = "";
		$this->_arguments = array();
	}
	
	public function setText( $str ){
		if( is_string($str) )
			$this->_text = $str;
	}
	
	public function setArgument( $name, $value ){
		$this->_arguments[$name] = $value;
	}
	
	public function getText(){
		return $this->_text;
	}
	
	public function parseText( $nl2br = true ){
		$search = array();
		$replace = array();
		
		$i = 0;
		foreach( $this->_arguments as $key => &$value ){
			$search[$i] = "%$key%";
			$replace[$i] = "$value";
			$i++;
		}
		
		if( $nl2br )
			return nl2br( str_replace($search,$replace,$this->_text) );
		else
			return str_replace( $search, $replace, $this->_text );
	}
}

?>